package com.walle.netty.proto;

import com.google.protobuf.InvalidProtocolBufferException;

/**
 * @author zhangjiapeng
 * @Package com.walle.netty.proto
 * @Description: ${todo}
 * @date 2019/1/28 15:57
 */
public class TestProto {

    private static byte[] encode(SubscribeReqProto.SubscribeReq req){
        return req.toByteArray();
    }

    private static SubscribeReqProto.SubscribeReq decode(byte[] body) throws InvalidProtocolBufferException {
        return SubscribeReqProto.SubscribeReq.parseFrom(body);
    }

    private static SubscribeReqProto.SubscribeReq createReq(){
        SubscribeReqProto.SubscribeReq.Builder builder = SubscribeReqProto.SubscribeReq.newBuilder();
        builder.setSubReqId(1);
        builder.setUserName("zhangjiapeng");
        builder.setProductName("IPhoneXS");
        builder.setAddress("beijing");
        return  builder.build();
    }

    public static void main(String[] args) throws InvalidProtocolBufferException {
        SubscribeReqProto.SubscribeReq req = createReq();
        System.out.println("编码前：" + req.toString());
        SubscribeReqProto.SubscribeReq decodeReq = decode(encode(req));
        System.out.println("解码后：" + decodeReq.toString());
        System.out.println("是否相等：" + req.equals(decodeReq));
    }

}
